﻿

namespace QFlow.Windows.Forms
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    using QFlow.Data;


    public partial class CustomersList : QFlow.Utils.BaseForm
    {
        public CustomersList(QFlowDbContext context)
            : base(context)
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            NewCustomer frm = new NewCustomer(true, this.Context);
            frm.MdiParent = this.MdiParent;
            frm.Show();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int customerId = (gvResults.SelectedRows[0].DataBoundItem as Customer).CustomerID;
                NewCustomer frm = new NewCustomer(customerId, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                try
                {
                    if (MessageBox.Show(QFlow.Windows.Properties.Resources.MBDeleteCustomer, QFlow.Windows.Properties.Resources.MBConfirmCaption, MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
                    {
                        Customer customer = gvResults.SelectedRows[0].DataBoundItem as Customer;
                        this.Context.Customers.DeleteOnSubmit(customer);
                        this.Context.SubmitChanges();
                        RefreshGrid();
                    }
                }
                catch
                {
                    MessageBox.Show(QFlow.Windows.Properties.Resources.MBCantDeleteCustomer, QFlow.Windows.Properties.Resources.MBErrorCaption, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                }
            }
        }

        private void gvResults_SelectionChanged(object sender, EventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                btnEdit.Enabled = true;
                btnDelete.Enabled = true;
            }
            else
            {
                btnEdit.Enabled = false;
                btnDelete.Enabled = false;
            }

        }

        private void CustomersList_Activated(object sender, EventArgs e)
        {
            RefreshGrid();
        }

        private void RefreshGrid()
        {
            IEnumerable<Customer> lst = this.Context.Customers.ToArray();
            if (!string.IsNullOrEmpty(txtName.TextBox.Text))
                lst = lst.Where(customer => customer.Name.Contains(txtName.TextBox.Text));

            gvResults.AutoGenerateColumns = false;
            gvResults.DataSource = lst.ToArray();
        }

        private void gvResults_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (gvResults.SelectedRows.Count > 0)
            {
                int customerId = (gvResults.SelectedRows[0].DataBoundItem as Customer).CustomerID;
                NewCustomer frm = new NewCustomer(customerId, this.Context);
                frm.MdiParent = this.MdiParent;
                frm.Show();
            }
        }

        private void gvResults_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            if (gvResults.Rows.Count > 0)
            {
                foreach (DataGridViewRow item in gvResults.Rows)
                {
                    Customer customer = item.DataBoundItem as Customer;
                    item.Cells["clmAmoun"].Value = customer.Accounts.First().Amount;
                }
            }
        }
    }
}
